package com.atguigu.jxc.service.impl;

import com.atguigu.jxc.dao.GoodsDao;
import com.atguigu.jxc.domain.ErrorCode;
import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.domain.SuccessCode;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.entity.Log;
import com.atguigu.jxc.service.CustomerReturnListGoodsService;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.service.LogService;
import com.atguigu.jxc.service.SaleListGoodsService;
import com.atguigu.jxc.util.BigDecimalUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description
 */
@Service
public class GoodsServiceImpl implements GoodsService {

    @Autowired
    private GoodsDao goodsDao;

    @Override
    public ServiceVO getCode() {

        // 获取当前商品最大编码
        String code = goodsDao.getMaxCode();

        // 在现有编码上加1
        Integer intCode = Integer.parseInt(code) + 1;

        // 将编码重新格式化为4位数字符串形式
        String unitCode = intCode.toString();

        for(int i = 4;i > intCode.toString().length();i--){

            unitCode = "0"+unitCode;

        }
        return new ServiceVO<>(SuccessCode.SUCCESS_CODE, SuccessCode.SUCCESS_MESS, unitCode);
    }

    //默认首页
    @Override
    public Map<String, Object> listInventory(Integer page, Integer rows, String codeOrName, Integer goodsTypeId) {
       Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
       List<Goods> goodsList=goodsDao.listInventory(offSet,rows,codeOrName,goodsTypeId);



       map.put("total",goodsList.size());
       map.put("rows",goodsList);
        return map;
    }

    //新增或修改商品信息
    @Override
    public Boolean save(Goods goods) {
        if(goods.getGoodsId()==null){
            //新增
            goodsDao.save(goods);
            return true;
        }else {

            goodsDao.update(goods);
            return true;
        }


    }

    //删除
    @Override
    public Boolean delete(Integer goodsId) {
        try {

           Integer integer= goodsDao.delete(goodsId);
           if(integer.intValue()>0){
               return true;
           }
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }


        return false;
    }

   // 无库存商品列表展示（可以根据商品名称或编码查询）
    @Override
    public Map<String, Object> getNoInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> goodsList=goodsDao.getNoInventoryQuantity(offSet,rows,nameOrCode);



        map.put("total",goodsList.size());
        map.put("rows",goodsList);
        return map;
    }

    //分页查询有库存商品信息
    @Override
    public Map<String, Object> getHasInventoryQuantity(Integer page, Integer rows, String nameOrCode) {
        Map<String, Object> map = new HashMap<>();
        page = page == 0 ? 1 : page;
        int offSet = (page - 1) * rows;
        List<Goods> goodsList=goodsDao.getHasInventoryQuantity(offSet,rows,nameOrCode);



        map.put("total",goodsList.size());
        map.put("rows",goodsList);
        return map;
    }

    // 添加库存、修改数量或成本价
    @Override
    public Boolean saveStock(Integer goodsId, Integer inventoryQuantity, double purchasingPrice) {

        try {
            BigDecimal bigDecimal= BigDecimal.valueOf(BigDecimalUtil.keepTwoDecimalPlaces(purchasingPrice));

            goodsDao.saveStock(goodsId,inventoryQuantity,bigDecimal);
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    //删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
    @Override
    public Boolean deleteStock(Integer goodsId) {
       Integer result= goodsDao.deleteStock(goodsId);
       if(result>0){
           return true;
       }
        return false;
    }

    //查询库存报警商品信息
    @Override
    public Map<String, Object> listAlarm() {
    Map<String, Object> hashMap = new HashMap<>();
        List<Goods> goodsList= goodsDao.listAlarm();
        hashMap.put("rows", goodsList);
        return hashMap;
    }


}
